package com.zsl.swing.redis.desktop.window.panel;

import com.zsl.swing.redis.desktop.model.KeyEntity;
import com.zsl.swing.redis.desktop.reference.RelativeLayout;
import com.zsl.swing.redis.desktop.window.builder.RedisTypeComboBoxBuilder;
import com.zsl.swing.redis.desktop.window.dialog.ZslKeyChangeDialog;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;

public class ZslKeyValuePanel extends JPanel {

    private JTextField keyField;

    private JComboBox<String> redisTypeComboBox = RedisTypeComboBoxBuilder.getInstance();

    private JTextArea valueArea ;

    private ZslKeyChangeDialog parent;

    public ZslKeyValuePanel(KeyEntity keyEntity, ZslKeyChangeDialog zslKeyChangeDialog){
        super();

        this.parent = zslKeyChangeDialog;

        int vFieldWidth = this.parent.getWidth() /2;

        this.keyField = new JTextField(vFieldWidth);
        this.valueArea = new JTextArea(15,vFieldWidth);
        this.valueArea.setLineWrap(true);

        JPanel panel = this.initRelativeLayoutPanel();
        this.setLayout(new BorderLayout());
        this.add(panel, BorderLayout.CENTER);

        if(!Objects.isNull(keyEntity)){
            this.keyField.setEditable(false);
            this.redisTypeComboBox.setEditable(false);
            this.redisTypeComboBox.setEnabled(false);
            this.initKeyValue(keyEntity);
        }


    }

    private JPanel initRelativeLayoutPanel(){
        RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
        rl.setFill(true);

        JPanel panel = new JPanel(rl);
        panel.add(Box.createGlue(), new Float(2));
        panel.add(this.getWidPanel(new JLabel("key:"), this.keyField), new Float(1));
        panel.add(this.getWidPanel(new JLabel("typ:"), this.redisTypeComboBox), new Float(1));
        panel.add(this.getWidPanel(new JLabel("val:"), new JScrollPane(this.valueArea)), new Float(5));
        panel.add(Box.createGlue(), new Float(2));

        return panel;

    }

    private JPanel getWidPanel(JLabel label, JComponent field){
        RelativeLayout rl = new RelativeLayout(RelativeLayout.X_AXIS);
        JPanel panel = new JPanel(rl);
        panel.add(Box.createGlue(),new Float(1));
        panel.add(label, new Float(1));
        panel.add(field, new Float(6));
        panel.add(Box.createGlue(),new Float(1));
        return panel;

    }

    private void initKeyValue(KeyEntity keyEntity) {
        if(!Objects.isNull(keyEntity)){
            this.keyField.setText(keyEntity.getKey());
            this.redisTypeComboBox.setSelectedItem(keyEntity.getType());

            String value = keyEntity.getValue();
            int length = value.length();
            if(length <= 10000){
                this.valueArea.setText(value);
            }else{
                int limit = 5000, start = 0;
                while(start < length){
                    this.valueArea.append(value.substring(start,Math.min(start + limit, length)));
                    start += limit;
                }
            }
        }
    }

    public String getKey() {
        return this.keyField.getText() == null? null: this.keyField.getText().trim();
    }

    public String getType() {
        return (String)this.redisTypeComboBox.getSelectedItem();
    }

    public String getValue() {
        return this.valueArea.getText() == null? null: this.valueArea.getText().trim();
    }
}
